import 'package:environmental_les_app/common/style/colours.dart';
import 'package:flutter_clx_base/common/colours.dart';
import 'package:flutter_clx_base/flutter_clx_base.dart';
import 'button_public_type.dart';

export 'button_public_type.dart';

/// 按钮样式
class ButtonPublicRect extends StatefulWidget {
  final String? text; // 按钮文本
  final VoidCallback? onPressed; // 点击事件
  final double? minWidth; // 最小宽度
  final double minHeight; // 最小高度
  final EdgeInsetsGeometry? margin; // 外边距
  final Color? borderColor; // 边框颜色
  final double borderWidth; // 边框宽度
  final Color? textColor; // 按钮文本颜色
  final double textSize; // 按钮文本字体大小
  final Color backgroundColor; // 按钮可点击颜色
  final FontWeight fontWeight; // 按钮文本字体粗细
  final double borderRadius; // 按钮圆角大小
  final ButtonPublicType type; // 按钮类型
  // 是否禁用效果
  final bool disabled;
  final int throttleTime; // 节流毫秒值
  final bool throttle; // 是否节流

  const ButtonPublicRect({
    super.key,
    required this.text,
    required this.onPressed,
    this.minWidth = 0.0,
    this.minHeight = 48.0,
    this.margin,
    this.textSize = 17.0,
    this.textColor,
    this.borderColor,
    this.borderWidth = 1.0,
    this.backgroundColor = Colours.appMain,
    this.fontWeight = FontWeight.bold,
    this.borderRadius = 8.0,
    this.type = ButtonPublicType.solid,
    this.disabled = false,
    this.throttle = false,
    this.throttleTime = 2000,
  });

  @override
  State<ButtonPublicRect> createState() => _ButtonPublicRectState();
}

class _ButtonPublicRectState extends State<ButtonPublicRect> {
  /// 当前点击时间
  DateTime? currentPressTime;

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: widget.margin,
      constraints: BoxConstraints(
          minWidth: widget.minWidth ?? 0.0, minHeight: widget.minHeight),
      child: ElevatedButton(
        onPressed: () {
          KeyBoardUtils.hideKeyboard();
          if (_canPressed()) {
            widget.onPressed?.call();
          }
        },
        style: ButtonStyle(
          padding: MaterialStateProperty.all(
              const EdgeInsets.symmetric(horizontal: 5.0)),
          textStyle: MaterialStateProperty.all(TextStyle(
              fontSize: widget.textSize, fontWeight: widget.fontWeight)),
          // 设置文字颜色
          foregroundColor: MaterialStateProperty.all(_textColor()),
          splashFactory: NoSplash.splashFactory,
          shadowColor: MaterialStateProperty.all(Colors.transparent),
          // 设置背景颜色
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            return _backgroundColor();
          }),
          //设置水波纹颜色
          overlayColor: MaterialStateProperty.all(
              !widget.disabled ? Colours.overlayColor : Colors.transparent),
          shape: MaterialStateProperty.all(
            RoundedRectangleBorder(
                borderRadius: BorderRadius.circular(widget.borderRadius)),
          ),
          side: MaterialStateProperty.all(
              BorderSide(color: _borderColor(), width: widget.borderWidth)),
        ),
        child: Text(widget.text ?? ""),
      ),
    );
  }

  /// 根据按钮类型设置按钮文本颜色
  /// 按钮类型为outline、dash 文本颜色设置边框颜色
  Color _textColor() {
    switch (widget.type) {
      case ButtonPublicType.solid:
        return widget.textColor ?? white;
      case ButtonPublicType.outline:
      case ButtonPublicType.dash:
        return widget.textColor ?? widget.borderColor ?? Colours.appMain;
      default:
        return widget.textColor ?? white;
    }
  }

  /// 根据按钮类型设置按钮可点击颜色
  /// 按钮类型为outline、dash 按钮可点击颜色为透明
  Color _backgroundColor() {
    switch (widget.type) {
      case ButtonPublicType.solid:
        return !widget.disabled ? widget.backgroundColor : Colors.black12;
      case ButtonPublicType.outline:
      case ButtonPublicType.dash:
        return Colors.white;
      default:
        return !widget.disabled ? widget.backgroundColor : Colors.black12;
    }
  }

  /// 根据按钮类型设置按钮边框颜色
  /// 按钮类型为outline 设置边框颜色
  Color _borderColor() {
    switch (widget.type) {
      case ButtonPublicType.outline:
        return widget.borderColor ?? Colours.appMain;
      default:
        return Colors.transparent;
    }
  }

  /// 是否可点击，防抖节流
  bool _canPressed() {
    if (widget.disabled) {
      return false;
    }
    DateTime now = DateTime.now();
    if (widget.throttle &&
        currentPressTime != null &&
        now.difference(currentPressTime!) <
            Duration(milliseconds: widget.throttleTime)) {
      // 节流时间，不可点击
      return false;
    }
    currentPressTime = now;
    return true;
  }
}
